$(function(){
    obj.init();
});

var obj = new JsFun();
function JsFun(){
    'use strict';
    return {
        params: {
            type: null,
            userIds: null
        },
        dom: {
            depList: null,
            confirm: null
        },
        // form: null,
        init: function () {
            var $this = this;

            this.params.type    = xhTool.getQuery('type');
            this.params.userIds = xhTool.getQuery('userIds');
            this.dom.confirm    = window.top.xhTop.getModalConfirmDom();

            this.dom.depList = $('#depList');
            this.dom.depList.treegrid({
                url: '/group/getAllDepGroup',
                queryParams: {
                    type: $this.params.type
                },
                // animate: false,
                onLoadSuccess: function (data) {
                    $this.funObj().initExistGroup();
                }
            });
            // this.form = new xhForm($('#form_group'));

            this.listener();
        },
        listener: function () {
            var $this = this;
            //确认分配角色
            $this.dom.confirm.click(function() {
                $this.funObj().submitGroup();
            });
        },
        funObj: function(){
            var $this = this;
            return {
                //单用户则选中已存在用户组
                initExistGroup: function () {
                    if ($this.params.userIds.indexOf(',') !== -1) {
                        return false;
                    }
                    xhExtra.ajaxRequest('group/getUserGroup', {type: $this.params.type, userId: $this.params.userIds}, {success: false}, function (data) {
                        $.each(data, function (key, groupId) {
                            $this.dom.depList.treegrid('select', 'group_' + groupId);
                        });
                    });
                },
                submitGroup: function() {
                    var rows     = $this.dom.depList.treegrid('getSelections'),
                        groupIds = [];
                    for(var i = 0; i < rows.length; i++){
                        if (rows[i].iGroupId) {
                            groupIds.push(rows[i].iGroupId);
                        }
                    }
                    xhExtra.ajaxRequest('group/saveUserGroup', $.extend({}, $this.params, {groupId: groupIds}), {success: false, check: false}, function (result) {
                        xhExtra.setBtnNormal($this.dom.confirm);
                        if (xhExtra.checkError(result)) return false;
                        window.top.xhTop.showSuccess(result.msg);
                        window.top.xhTop.closeModal();
                    });
                    // $this.form.submit(baseUrl + 'Group/saveUserGroup', {type: $this.params.type, userIds: $this.params.userIds}, function(result){
                    //     xhExtra.setBtnNormal($this.dom.confirm);
                    //     if (xhExtra.checkError(result)) return false;
                    //     window.top.xhTop.showSuccess(result.msg);
                    //     window.top.xhTop.closeModal();
                    // });
                }
            }
        }
    }
}